package serv.midser.wpush;

import java.io.IOException;
import java.util.Map;

import serv.utils.HttpUtils;
import serv.utils.JsonUtils;
import serv.web.api.adapter.ServWebApiTokenAdapterService;

/**
 * wpush服务中间件适配器
 * 
 * 提供业务服务器访问中间件的方法
 * 
 * @author shiying
 *
 */
public class WPushServerAdapter {
	
	/**
	 * 静态资源基础路径
	 */
	private String baseApiServerUrl;
	
	/**
	 * 通知服务URL地址
	 */
	private String noticeUrl = "api/notice.do";
	
	/**
	 * 消息监听服务基础地址
	 */
	private String baseListenServerUrl;
	
	/**
	 * 消息监听URL地址
	 */
	private String listenUrl = "serv/wpush/amq";
	
	
	/**
	 * ApiToken服务
	 */
	private ServWebApiTokenAdapterService servWebApiTokenAdapterService;
	
	
	/**
	 * 发送通知
	 * @param message
	 * @throws IOException 
	 */
	public void sendServerNotice(String token, String message) throws IOException {
		String url = baseApiServerUrl+noticeUrl+"?token="+token;
		String res = HttpUtils.post(url, message);
		Map<String, Object> resObj = JsonUtils.parseJsonMap(res);
		if (!"success".equals(resObj.get("errcode"))) {
			throw new RuntimeException("send wpush notice faild. msg >> " + (String) resObj.get("errmsg"));
		}
	}
	
	/**
	 * 请求Token
	 * @param clientId
	 * @return
	 * @throws IOException
	 */
	public String getClientListenToken(String clientId) throws IOException {
		return servWebApiTokenAdapterService.apiTokenRequest(clientId);
	}

	/**
	 * 请求客户端消息监听的URL地址
	 * @param url
	 * @return
	 */
	public String getServerListenUrl() {
		return baseListenServerUrl + listenUrl;
	}


	public String getBaseApiServerUrl() {
		return baseApiServerUrl;
	}

	public void setBaseApiServerUrl(String baseApiServerUrl) {
		this.baseApiServerUrl = baseApiServerUrl;
	}

	public String getNoticeUrl() {
		return noticeUrl;
	}

	public void setNoticeUrl(String noticeUrl) {
		this.noticeUrl = noticeUrl;
	}

	public ServWebApiTokenAdapterService getServWebApiTokenAdapterService() {
		return servWebApiTokenAdapterService;
	}


	public void setServWebApiTokenAdapterService(ServWebApiTokenAdapterService servWebApiTokenAdapterService) {
		this.servWebApiTokenAdapterService = servWebApiTokenAdapterService;
	}

	public String getBaseListenServerUrl() {
		return baseListenServerUrl;
	}

	public void setBaseListenServerUrl(String baseListenServerUrl) {
		this.baseListenServerUrl = baseListenServerUrl;
	}

	public String getListenUrl() {
		return listenUrl;
	}

	public void setListenUrl(String listenUrl) {
		this.listenUrl = listenUrl;
	}
	
	

}
